// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.render;

import lichen.LichenConstants;

import org.apache.tapestry.internal.services.PageTemplateLocator;
import org.apache.tapestry.internal.services.RequestEncodingInitializerImpl;
import org.apache.tapestry.ioc.Configuration;
import org.apache.tapestry.ioc.MappedConfiguration;
import org.apache.tapestry.ioc.annotations.InjectService;
import org.apache.tapestry.services.AliasContribution;
import org.apache.tapestry.services.AssetFactory;
import org.apache.tapestry.services.ComponentClassResolver;

/**
 * 针对render的处理的模块.
 * <p>
 * <a href="http://issues.apache.org/jira/browse/TAPESTRY-1294>Tapestry-1294<a>已经纠正了，
 * 所以先在不需要显示的申明为UTF-8了，默认情况已经为UTF-8.
 * </p>
 * 关于表单的提交,也解决了,在
 * {@link RequestEncodingInitializerImpl#initializeRequestEncoding(String)}中已经根据encoding
 * 自动设定编码了.<br/> 在{@link org.apache.tapestry.services.TapestryModule#contributeMetaDataLocator(org.apache.tapestry.ioc.MappedConfiguration)}
 * 中已经定义默认为
 * text/html;charset=utf-8,但是conponentAction之前在jetty上没问题，但是在tomcat上就出问题了.
 * </p>
 * 
 * @author Jun Tsai
 * @version $Revision: 175 $
 * @since 0.0.3
 */
public class RenderModule {



	/**
	 * 默认的encoding设置为UTF-8
	 * 
	 * @param configuration
	 *            配置.
	 */
	public void contributeApplicationDefaults(
			MappedConfiguration<String, String> configuration) {
		// 默认的配置页面
		configuration.add(LichenConstants.DEFAULT_ENCODING, "utf-8");
	}
	/**
	 * 构建基于context的PageTemplateLocator,非WEB-INF目录下面.
	 */
	public PageTemplateLocator buildContextRootTemplateLocator(
			@InjectService("ContextAssetFactory")
			AssetFactory contextAssetFactory,

			ComponentClassResolver componentClassResolver) {
		return new ContextRootTemplateLocator(contextAssetFactory
				.getRootResource(), componentClassResolver);
	}

	/**
	 * 复写系统提供的Page Template Locator.
	 * @param configuration 配置文件
	 * @param contextRootTemplateLocator context root template locator.
	 * @since 0.0.3 
	 */
	public static void contributeAlias(
			Configuration<AliasContribution> configuration,
			@InjectService("ContextRootTemplateLocator")
			PageTemplateLocator contextRootTemplateLocator) {
		configuration.add(AliasContribution.create(PageTemplateLocator.class,
				contextRootTemplateLocator));
	}
}
